package com.jason.quectel0805.activities;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.jason.quectel0805.R;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * @author：Jason
 * @date：2022/8/9 23:59
 * @email：1129847330@qq.com
 * @description:
 */
public class SharedPreferencesActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);
    }

    public void saveData(View view) {
        SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("name", "jason");
        editor.putString("age", "18");
        editor.putString("sex", "男");
        editor.commit();
    }

    public void readData(View view) {
        SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);
        String name = sharedPreferences.getString("name", "佚名");
        String age = sharedPreferences.getString("age", "0");
        String sex = sharedPreferences.getString("sex", "未知");
        Toast.makeText(this, name + ", " + age + ", " + sex, Toast.LENGTH_SHORT).show();
    }
}